<?php if (!defined('NOVA_RUN')) die('You can\'t access this file directly!');

/*
-------------------------------------------------------
*
*   NovaBoard 2
*   Copyright © 2011 NovaBoard team
*
*--------------------------------------------------------
*
*   Official site: www.novaboard.net
*   Contact e-mail: support@novaboard.net
*
*   GNU General Public License, version 2:
*   http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*
---------------------------------------------------------
*/

function exceptionHandler(Exception $e)
{
	echo $e->getMessage(), '<br /><br />';
	echo '<b>Stack Trace:</b>', str_replace('#', '<br />#', $e->getTraceAsString());
	
	header('HTTP/1.1 200 OK');
	ob_end_flush();
	exit;
}

function get_cookie($name, $validate = true)
{
	if (isset($_COOKIE[$name]))
	{
		return ($validate !== false) ? htmlspecialchars($_COOKIE[$name], ENT_QUOTES) : $_COOKIE[$name];
	}
	else
	{
		return null;
	}
}

function get_request($key, $type=null, $validate = true)
{
	switch ($type)
	{
		case null:
			$storage = &$_REQUEST;
			break;
		case 'get':
			$storage = &$_GET;
			break;
		case 'post':

			$storage = &$_POST;
			break;
	}

	if (isset($storage[$key]))
	{
		return ($validate !== false) ? htmlspecialchars(trim($storage[$key]), ENT_QUOTES) : trim($storage[$key]);
	}
	else
	{
		return null;
	}
}

function check_request($request, $type, $min=1, $max=10000)
{
	$request = htmlspecialchars_decode($request, ENT_QUOTES);
	switch($type)
	{
		case 'email': if (preg_match("/^[\da-z\_\-\.\+]+@[\da-z_\-\.]+\.[a-z]{2,5}$/i",$request)){ return true; } break;
		case 'md5': if (preg_match("/^[\da-z]{32}$/i",$request)){ return true; } break;
		case 'string': if (mb_strlen($request)>=$min && mb_strlen($request)<=$max){ return true; } break;
		case 'integer': if (mb_strlen($request)>=$min && mb_strlen($request) <=$max && is_numeric($request)){ return true; } break;
		default: return false;
	}
	return false;
}

function make_link($action = null, $method = null, array $params = array())
{
	$url = URL;
	
	if ($action != null)
	{
		$url .= 'index.php/' . $action;

		if ($method != null)
		{
			$url .= '/' . $method;
		}

		if (!empty($params) && $params[0] != null)
		{
			foreach ($params as $param)
			{
				$url .= '/' . $param;
			}
		}

		if (Config::get('page_extension') != '')
		{
			$url .= '.' . Config::get('page_extension');
		}
	}
	
	return $url;
}

function make_admin_link($action = null, $method = null, array $params = array())
{
	global $config;
	
	$url = URL;
	
	if ($action != null)
	{
		$url .= 'admin.php/' . $action;

		if ($method != null)
		{
			$url .= '/' . $method;
		}

		if (!empty($params) && $params[0] != null)
		{
			foreach ($params as $param)
			{
				$url .= '/' . $param;
			}
		}

		if (Config::get('page_extension') != '')
		{
			$url .= '.' . Config::get('page_extension');
		}
	}
	else
	{
		$url .= 'admin.php';
	}
	return $url;
}

function make_rand($length)
{
	return substr(md5(uniqid(mt_rand(),true)),0,$length);
}

function redirect($url)
{
	if (!$url)
	{
		$url = make_link();
	}

	header('HTTP/1.1 301 Moved Permanently'); 	
    header('Location: ' . $url);
    exit;
}

/**
 * Template wrapper function to access settings.
 * @param string $setting
 * @return string
 */
function setting($setting)
{
	return Config::get($setting);
}
